package com.example.demo2.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo2.entity.Customer;
import com.example.demo2.entity.DTO.QuestionDTO;
import com.example.demo2.entity.Question;
import com.example.demo2.mapper.CustomerMapper;
import com.example.demo2.mapper.QuestionMapper;
import org.apache.ibatis.jdbc.Null;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class QuestionService extends ServiceImpl<QuestionMapper, Question> {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private CustomerMapper customerMapper;

    public Map<String, Object> getALlByQId(QuestionDTO questionDTO) {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<Question> lqw = new LambdaQueryWrapper<>();
        lqw.ne(Question::getPId, 0);
        lqw.eq(Question::getQId,questionDTO.getQId());
        lqw.orderByDesc(Question::getCreateTime);
        Page<Question> iPage = new Page<>(questionDTO.getPage(), questionDTO.getLimit());
        Page<Question> page = page(iPage, lqw);
        page.getRecords().forEach(item->{
            LambdaQueryWrapper<Customer> lqw2 = new LambdaQueryWrapper<>();
                lqw2.eq(Customer::getId, item.getUserId());
                Customer customer = customerMapper.selectOne(lqw2);
                item.setUserInfo(customer);
        });
        LambdaQueryWrapper<Question> lqw3 = new LambdaQueryWrapper<>();
        lqw3.eq(Question::getPId, 0);
        lqw3.eq(Question::getQId,questionDTO.getQId());

        Question question = questionMapper.selectOne(lqw3);
        question.setAnswerNum((int) page.getTotal());
        question.setView(question.getView() + 1);
        questionMapper.updateById(question);

        LambdaQueryWrapper<Customer> lqw4 = new LambdaQueryWrapper<>();
        lqw4.eq(Customer::getId, question.getUserId());
        Customer customer = customerMapper.selectOne(lqw4);
        question.setUserInfo(customer);

        map.put("question",question);
        map.put("answers",page);
        return map;
    }

    public IPage<Question> getAllQuestion(QuestionDTO questionDTO) {
        LambdaQueryWrapper<Question> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Question::getPId, 0);
        lqw.orderByDesc(Question::getCreateTime);
        lqw.like(Strings.isNotEmpty(questionDTO.getUsername()), Question::getUsername, questionDTO.getUsername());
        lqw.like(Strings.isNotEmpty(questionDTO.getContent()), Question::getContent, questionDTO.getContent());
        Page<Question> iPage = new Page<>(questionDTO.getPage(), questionDTO.getLimit());
        return page(iPage, lqw);
    }

}